package com.winhxd.b2c.product.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.product.condition.ProductAttributeAndOptionCondition;
import com.winhxd.b2c.common.domain.product.condition.ProductAttributeCondition;
import com.winhxd.b2c.common.domain.product.model.ProductAttribute;
import com.winhxd.b2c.common.domain.product.model.ProductAttributeOption;
import com.winhxd.b2c.common.domain.product.vo.ProductAttributeAndOptionVO;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.product.dao.ProductAttributeMapper;
import com.winhxd.b2c.product.dao.ProductAttributeOptionMapper;
import com.winhxd.b2c.product.service.AttributeManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author cuipengbo
 * @create 2019-01-07 11:48
 * @desc 商品属性管理
 **/
@Service
public class AttributeManagementServiceImpl implements AttributeManagementService {

    private Logger logger = LoggerFactory.getLogger(AttributeManagementServiceImpl.class);

    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    @Autowired
    private ProductAttributeOptionMapper productAttributeOptionMapper;

    @Override
    public PagedList<ProductAttribute> findProductAttributeList(ProductAttributeCondition productAttributeCondition) {
        logger.info("findProductAttributeList方法入参是：" + JsonUtil.toJSONString(productAttributeCondition));
        Page page = PageHelper.startPage(productAttributeCondition.getPageNo(), productAttributeCondition.getPageSize());
        List<ProductAttribute> productList = productAttributeMapper.selectProductAttributeList();
        PagedList<ProductAttribute> pagedList = new PagedList<>();
        pagedList.setData(productList);
        pagedList.setPageNo(productAttributeCondition.getPageNo());
        pagedList.setPageSize(productAttributeCondition.getPageSize());
        pagedList.setTotalRows(page.getTotal());
        return pagedList;
    }

    @Override
    public Integer modifyProductAttributeById(ProductAttributeCondition productAttributeCondition) {
        logger.info("modifyProductAttributeById方法入参是：" + JsonUtil.toJSONString(productAttributeCondition));
        ProductAttribute productAttribute = new ProductAttribute();
        BeanUtils.copyProperties(productAttributeCondition, productAttribute);
        return productAttributeMapper.updateByPrimaryKeySelective(productAttribute);
    }

    @Override
    public ProductAttributeAndOptionVO getProductAttributeById(Long id) {
        ProductAttributeAndOptionVO attributeAndOptionvo = new ProductAttributeAndOptionVO();
        // 查询商品属性
        ProductAttribute productAttribute = productAttributeMapper.selectByPrimaryKey(id);
        attributeAndOptionvo.setAttributeName(productAttribute.getAttributeName());
        attributeAndOptionvo.setId(productAttribute.getId());
        //根据属性id查询属性值(目前正序)
        List<ProductAttributeOption> options = productAttributeOptionMapper.selectAttributeOptionByAttributeId(productAttribute.getId());
        attributeAndOptionvo.setAttributeOptions(options);
        return attributeAndOptionvo;
    }

    @Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
    @Override
    public Integer saveOrUpdateProductAttribute(ProductAttributeAndOptionCondition condition) {
        logger.info("saveOrUpdateProductAttribute方法入参是：" + JsonUtil.toJSONString(condition));
        Integer i = 1;
        try {
            // 存在则修改
            if(condition.getId() != null){
                //修改属性名称
                ProductAttribute attribute = new ProductAttribute();
                attribute.setId(condition.getId());
                attribute.setAttributeName(condition.getAttributeName());
                productAttributeMapper.updateByPrimaryKeySelective(attribute);
                // 修改属性值或添加属性值
                List<ProductAttributeOption> attributeOptions = condition.getAttributeOptions();
                attributeOptions.stream().forEach(option ->{
                    ProductAttributeOption productAttributeOption = productAttributeOptionMapper.selectByPrimaryKey(option.getId());
                    //存在则是修改
                    if(productAttributeOption != null){
                        productAttributeOptionMapper.updateOptionById(option.getId(), option.getOptionValue());
                    }else {
                        option.setAttributeId(condition.getId());
                        option.setState(1);
                        // 查询该属性下最大排序
                        int sortNo = productAttributeOptionMapper.selectMaxSortNoByAttributeId(condition.getId());
                        option.setSortNo(sortNo + 1);
                        productAttributeOptionMapper.insertSelective(option);
                    }
                });
            }else {
                //不存在则添加
                ProductAttribute attribute = new ProductAttribute();
                attribute.setState(1);
                attribute.setType(1);
                attribute.setAttributeName(condition.getAttributeName());
                //添加且查询新增的属性id
                productAttributeMapper.insertSelective(attribute);
                List<ProductAttributeOption> attributeOptions = condition.getAttributeOptions();
                if(!CollectionUtils.isEmpty(attributeOptions)){
                    Integer sort = 0;
                    for(ProductAttributeOption option : attributeOptions){
                        option.setAttributeId(attribute.getId());
                        option.setState(1);
                        // 查询该属性下最大排序
                        sort ++ ;
                        option.setSortNo(sort);
                        productAttributeOptionMapper.insertSelective(option);
                    };
                }
            }
        }catch (Exception e){
            i = 0;
            logger.error("保存属性方法失败", e);
        }
        return i;

    }

    @Override
    public List<ProductAttribute> findProductAttributeByModel(ProductAttributeCondition condition) {
        ProductAttribute productAttribute = new ProductAttribute();
        BeanUtils.copyProperties(condition, productAttribute);
        return productAttributeMapper.selectProductAttributeByModel(productAttribute);
    }
}
